//
//  CHButtonListView.swift
//  CHSimpleUI
//
//  Created by cyptt on 2024/4/22.
//

import UIKit
public enum CH_SIMPLE_BTN_POSITION : Int {
    case left = 0  //左边
    case center = 1 //中间
    case right = 2 //右边

}
public class CHSimpleBtnListView: UIView {

    //   public var minimumLineSpacing: CGFloat = 0  //行间距
    
    public var minimumInteritemSpacing: CGFloat?  //item 距离 该只不传默认不设置间距
    
    public var isEdging:Bool = true //默认贴边,否则等分。设置了minimumInteritemSpacing 则无效。
    
    public  var itemSize: CGSize?  //不传默认等分
    @objc dynamic public  var dataSource:[String] = [] //kvo 需要添加@objc dynamic
    
    var selfW:CGFloat = 0
    var selfH:CGFloat =  0
    var btnArr:[UIButton] = []
    
    
    private let btnTag:Int = 9000
    public var positionType:CH_SIMPLE_BTN_POSITION = .center
    
    public init(){
        super.init(frame: .zero)
        self.setUp()
   
    }
    public override func awakeFromNib(){
         super.awakeFromNib()
         self.setUp()
     }
    public override init(frame: CGRect) {
         super.init(frame: frame)
         self.setUp()
     }
     
    public required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
    }
     
    

    
   public func refreshData(dataArr:[String]){
        
        self.btnArr.forEach { btn in
            btn.removeFromSuperview()
           
        }
       self.dataSource.removeAll()
        self.dataSource = dataArr
        
        self.reloadData()
    }
    
   public override func layoutSubviews() {
        super.layoutSubviews()
        self.selfW = self.frame.size.width
        self.selfH = self.frame.size.height
    }
    
    
    
    public func reloadData(){
    
        //加这一句执行layousubviews
        self.layoutIfNeeded()
     
        
        let count = self.dataSource.count
        let itemContentW:CGFloat = self.selfW / CGFloat(count)
        let itemContentH:CGFloat = selfH
        var i = 0
        
        var itemW = itemContentW
        var itemH = itemContentH
        if self.itemSize != nil{
          
            itemW = self.itemSize!.width
            itemH = self.itemSize!.height
        }else{
            itemW = itemContentW
            itemH = itemContentH
        }
        dataSource.forEach { item in

            let btn:UIButton = UIButton.init(type: .custom)
            btn.setTitle(item, for: .normal)
            self.addSubview(btn)
            btn.addTarget(self, action: #selector(btnClick(btn: )), for: .touchUpInside)
            btn.tag = btnTag + i
            var x:CGFloat = 0
            var marginLeftX:CGFloat = 0
            var spaceX:CGFloat = 0
            if self.positionType == .center{
          
                if minimumInteritemSpacing == nil{
                   
    
                    if self.isEdging == true {
                        spaceX = (selfW - itemW*CGFloat(count))/CGFloat(count - 1)
                        marginLeftX = 0
                    }else{
                        spaceX = (selfW - itemW*CGFloat(count))/CGFloat(count+1)
                        marginLeftX = spaceX
                    }
                    
                    x = itemW*CGFloat(i) + spaceX*CGFloat(i) + marginLeftX
                  
                }else{
                    let spaceX:CGFloat = minimumInteritemSpacing!
                    marginLeftX  = (selfW - (itemW*CGFloat(count) + spaceX*CGFloat((count-1))))/2
                    
                    x =  itemW*CGFloat(i) + spaceX*CGFloat(i) + marginLeftX
                }
            
              
            }else if self.positionType == .left{
            
                if minimumInteritemSpacing == nil{
                   

                    spaceX = (selfW - itemW*CGFloat(count))/CGFloat(count - 1)
                    marginLeftX = 0
                    
                    x = itemW*CGFloat(i) + spaceX*CGFloat(i) + marginLeftX
                  
                }else{
                    if self.isEdging == true{
                        spaceX = minimumInteritemSpacing!
                        marginLeftX = 0
                    }else{
                        spaceX = minimumInteritemSpacing!
                        marginLeftX  = minimumInteritemSpacing!
                        
                    }
                 
                    x =  itemW*CGFloat(i) + spaceX*CGFloat(i) + marginLeftX
                }
            }else if self.positionType == .right{
                if minimumInteritemSpacing == nil{
                   

                    spaceX = (selfW - itemW*CGFloat(count))/CGFloat(count - 1)
                    marginLeftX = 0
                    
                    x = itemW*CGFloat(i) + spaceX*CGFloat(i) + marginLeftX
                  
                }else{
                    if self.isEdging == true{
                        spaceX = minimumInteritemSpacing!
                        marginLeftX = selfW  - itemW*CGFloat(count) - spaceX*CGFloat(count - 1)
                    }else{
                        spaceX = minimumInteritemSpacing!
                        marginLeftX = selfW  - itemW*CGFloat(count) - spaceX*CGFloat(count)
                        
                    }
                 
                    x =  itemW*CGFloat(i) + spaceX*CGFloat(i) + marginLeftX
                }
            }
            btn.frame = CGRect.init(x: x, y: (selfH - itemH)/2, width: itemW, height: itemH)
         
            i = i + 1
            self.btnArr.append(btn)
        }
    }
   
   public func refreshItemUI(closure:@escaping(_ btns:[UIButton])-> Void){
        closure(self.btnArr);
    }
    
 
    
    private var btnClickClosure:((_ index:Int,_ title:String) -> ())?
    public func btnClickFun(clouse:@escaping(_ index:Int,_ title:String)-> Void){
        self.btnClickClosure = clouse
    }
     func setUp(){

         
         
//         self.addObserver(self, forKeyPath: "dataSource",options: [.new,.old],context: nil)
      
     }
     
     deinit{
     
     }
  
    

//    public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//        print(object)
//        print(keyPath)
//    }

}


public extension CHSimpleBtnListView{
    @objc func btnClick(btn:UIButton){
  
        let index = btn.tag - btnTag
        print(index)
        if self.btnClickClosure != nil{
            self.btnClickClosure!(index,self.dataSource[index])
        }
     
      
    }
    
    @objc func dataSoueceChange(noti:NSNotification){
        
    }
}



